package lib;

import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import pojo.Website;

import dao.WebsiteDAO;

public class InteractiveTableModel extends AbstractTableModel{
	public static final int WEBSITEID_INDEX = 0;
	public static final int WEBSITE_INDEX = 1;
	public static final int MENU1_INDEX = 2;
	public static final int MENU2_INDEX = 3;
	public static final int PAGE_INDEX = 4;
	public static final int WEBCONTENT_INDEX = 5;
	public static final int IMAGE_INDEX = 6;
	public static final int BOOKCONTENT_INDEX = 7;
	public static final int BOOKINFO_INDEX = 8;
	public static final int WEBTYPE_INDEX = 9;
	public static final int ACTIVE_INDEX = 10;
	protected String[] columnNames;
	protected Vector<Website> dataVector;
	
	public InteractiveTableModel(String[] columnNames){
		this.columnNames = columnNames;
		dataVector = new Vector<Website>();
		WebsiteDAO websitedao = new WebsiteDAO();
		List<Website> websites = websitedao.getList();
		dataVector.addAll(websites);
	}
	public String getColumnName(int column){
		return columnNames[column];
	}
	public Class getColumnClass(int column){
		switch(column){
		case WEBSITEID_INDEX:
			return Integer.class;
		case WEBSITE_INDEX:
		case MENU1_INDEX:
		case MENU2_INDEX:
		case PAGE_INDEX:
		case WEBCONTENT_INDEX:
		case IMAGE_INDEX:
		case BOOKCONTENT_INDEX:
		case BOOKINFO_INDEX:
		case WEBTYPE_INDEX:
		case ACTIVE_INDEX:	
			return String.class;
		default:
			return Object.class;
		}
	}
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return columnNames.length;
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return dataVector.size();
	}

	@Override
	public Object getValueAt(int row, int column) {
		// TODO Auto-generated method stub
		Website website = dataVector.get(row);
		switch(column){
		case WEBSITEID_INDEX:
			return website.getWebsiteId();
		case WEBSITE_INDEX:
			return website.getWebsite();
		case MENU1_INDEX:
			return website.getMenu1();
		case MENU2_INDEX:
			return website.getMenu2();
		case PAGE_INDEX:
			return website.getPage();
		case WEBCONTENT_INDEX:
			return website.getWebContent();
		case IMAGE_INDEX:
			return website.getImage();
		case BOOKCONTENT_INDEX:
			return website.getBookContent();
		case BOOKINFO_INDEX:
			return website.getBookInfo();
		case WEBTYPE_INDEX:
			return website.getWebType();
		case ACTIVE_INDEX:
			return website.getActive();
		default:
			return website;	
		}
	}
	public void setValueAt(Object value, int row, int column){
		Website website = dataVector.get(row);
		switch(column){
		case WEBSITEID_INDEX:
			website.setWebsiteId((Integer)value);
			break;
		case WEBSITE_INDEX:
			website.setWebsite((String)value);
			break;
		case MENU1_INDEX:
			website.setMenu1((String)value);
			break;
		case MENU2_INDEX:
			website.setMenu2((String)value);
			break;
		case PAGE_INDEX:
			website.setPage((String)value);
			break;
		case WEBCONTENT_INDEX:
			website.setWebContent((String)value);
			break;
		case IMAGE_INDEX:
			website.setImage((String)value);
			break;
		case BOOKCONTENT_INDEX:
			website.setBookContent((String)value);
			break;
		case BOOKINFO_INDEX:
			website.setBookInfo((String)value);
			break;
		case WEBTYPE_INDEX:
			website.setWebType((String)value);
			break;
		default:
			break;
		}
		fireTableCellUpdated(row, column);
	}
	
	public boolean isCellEditable(int row, int column){
		return true;
	}
	public boolean hasEmptyRow(){
		if(dataVector.size() == 0)
			return true;
		return false;
	}
	public void addEmptyRow(){
		dataVector.add(new Website());
	}

}
